# Replication materials ---------------------------------------------------
# Paper:    Campaigns and the Selection of Policy-seeking Representatives.
#           Legislative Studies Quarterly
# Authors: Shaun Bowler, Gail McElroy, Stefan Müller


#--------------------------------------------------------------------------
# Script for creating master data (Dail Eireann 31, 2011-2016)
#--------------------------------------------------------------------------

# Load necessary packages -------------------------------------------------

library(car)
library(foreign)
library(tidyverse)

# Import original files into R environment -----------------------------

# Import the CCS 2012 survey (31st Dáil). 
# NOTE: THIS FILE CANNOT BE INCLUDED IN THE REPLICATION MATERIAL

# I manually added the pid column to merge the files afterwards and removed the labels in Stata
# IF YOU HAVE ACCESS TO THE DATA AND REQUIRE THE PID VARIABLE, PLEASE CONTACT THE AUTHORS
CCS31 <- read.dta("data_ireland_2011-2016/CCS_Ireland_2012_nolabels.dta")

# Create dummy variably "respondent" to check the difference in activity between people who 
# participated in survey and those who did not. 
CCS31$respondent <- factor(1)

# Recode some variables for the regression with wonseat as DV
CCS31 <- CCS31 %>% 
  mutate(wonseatCCS = win,
         partyCCS = car::recode(A1, "6='Independent'; 2='Fianna Fail';
                                3='Fine Gael'; 7='Labour Party'; 5='Greens';
                                8='Sinn Fein';9='ULA';1='Other';4='Other';10='Other'")) %>% 
  mutate(ageCCS = 2011 - E2) %>% 
  mutate(genderCCS = car::recode(E1, "2='male'; 1='female'")) %>% 
  mutate(incumbentPrevCCS = car::recode(A5r3, "NA='no';0='no';else='yes'"))

# Create campaign index ---------------------------------------------------

# Classic campaigning
CCS31$use_callingvoters <- car::recode(CCS31$B5ar1, "2:5=1; 1=0")
CCS31$use_debating <- car::recode(CCS31$B5ar2, "2:5=1; 1=0")
CCS31$use_partyofficials <- car::recode(CCS31$B5ar4, "2:5=1; 1=0")

# How much time after 1 February
CCS31$use_appointmentsclubs <- car::recode(CCS31$B5br1, "2:5=1; 1=0")
CCS31$use_townhallmeetings <- car::recode(CCS31$B5br2, "2:5=1; 1=0")
CCS31$use_localnewspaper <- car::recode(CCS31$B5br3, "2:5=1; 1=0")
CCS31$use_natnewspaper <- car::recode(CCS31$B5br4, "2:5=1; 1=0")
CCS31$use_localtvradio <- car::recode(CCS31$B5br5, "2:5=1; 1=0")
CCS31$use_nattvradio <- car::recode(CCS31$B5br6, "2:5=1; 1=0")


# Promotion material 
CCS31$use_posters <- car::recode(CCS31$B6r1a, "2=1; 1=0")
CCS31$use_billboards <- car::recode(CCS31$B6r2a, "2=1; 1=0")
CCS31$use_adsinnews <- car::recode(CCS31$B6r3a, "2=1; 1=0")
CCS31$use_officehours <- car::recode(CCS31$B6r4a, "2=1; 1=0")
CCS31$use_socialgatherings <- car::recode(CCS31$B6r5a, "2=1; 1=0")
CCS31$use_flyers <- car::recode(CCS31$B6r6a, "2=1; 1=0")

# Post-modern campaigning
CCS31$use_website <- car::recode(CCS31$B6r7a, "2=1; 1=0")
CCS31$use_emaillist <- car::recode(CCS31$B9r2, "2=1; 1=0")
CCS31$use_campaignvideo <- car::recode(CCS31$B9r3, "2=1; 1=0")
CCS31$use_onlinechats <- car::recode(CCS31$B9r4, "2=1; 1=0")
CCS31$use_blog <- car::recode(CCS31$B9r6, "2=1; 1=0")
CCS31$use_twitter <- car::recode(CCS31$B9r7, "2=1; 1=0")

# Create additive indices

CCS31$use_classic <- rowSums(CCS31[,c("use_callingvoters",
                                       "use_debating",
                                       "use_partyofficials",
                                       "use_appointmentsclubs",
                                       "use_townhallmeetings",
                                       "use_localnewspaper",
                                       "use_natnewspaper",
                                       "use_localtvradio",
                                       "use_nattvradio")], na.rm=TRUE)

CCS31$use_material <- rowSums(CCS31[,c("use_posters",
                                       "use_billboards",
                                       "use_adsinnews",
                                       "use_officehours",
                                       "use_socialgatherings",
                                       "use_flyers")], na.rm=TRUE)



CCS31$use_postmodern <- rowSums(CCS31[,c("use_website",
                                       "use_emaillist",
                                       "use_campaignvideo",
                                       "use_onlinechats",
                                       "use_blog",
                                       "use_twitter")], na.rm=TRUE)

CCS31$use_campaign <- rowSums(CCS31[,c("use_classic",
                                       "use_material",
                                       "use_postmodern")], na.rm = TRUE)


# Recode variables for ordinal index --------------------------------------

CCS31$use_ord_callingvoters <- car::recode(CCS31$B5ar1, "1=0")
CCS31$use_ord_debating <- car::recode(CCS31$B5ar2, "1=0")
CCS31$use_ord_internet <- car::recode(CCS31$B5ar3, "1=0")
CCS31$use_ord_partyofficials <- car::recode(CCS31$B5ar4, "1=0")
CCS31$use_ord_appointmentsclubs <- car::recode(CCS31$B5br1, "1=0")
CCS31$use_ord_townhallmeetings <- car::recode(CCS31$B5br2, "1=0")
CCS31$use_ord_localnewspaper <- car::recode(CCS31$B5br3, "1=0")
CCS31$use_ord_natnewspaper <- car::recode(CCS31$B5br4, "1=0")
CCS31$use_ord_localtvradio <- car::recode(CCS31$B5br5, "1=0")
CCS31$use_ord_nattvradio <- car::recode(CCS31$B5br6, "1=0")


CCS31$use_ord_total <- rowSums(CCS31[,c("use_ord_callingvoters",
                                      "use_ord_debating",
                                      "use_ord_internet",
                                      "use_ord_partyofficials",
                                      "use_ord_appointmentsclubs",
                                      "use_ord_townhallmeetings",
                                      "use_ord_localnewspaper",
                                      "use_ord_natnewspaper",
                                      "use_ord_localtvradio",
                                      "use_ord_nattvradio")], na.rm=TRUE)

CCS31$party_factor <- as.factor(CCS31$A1)

# Load data with legislative activity ----
Dail31 <- read.csv("data_ireland_2011-2016/dail_31_activity.csv")

# Calculate age

Dail31$age <- 2007 - Dail31$year_of_birth

Dail31$age_squared <- (Dail31$age)^2

# Merge datasets 
Dail31_Survey <- dplyr::full_join(Dail31, CCS31, by = "pid" )

# Import file with party information
Dail31parties <- read.csv("data_ireland_2011-2016/dail_30_parties.csv")
Dail31_Survey <- dplyr::left_join(Dail31_Survey, 
                                  Dail31parties, by = "party_abbr" )

# Change "respondent" variable (NA to 0)
Dail31_Survey$respondent <- car::recode(Dail31_Survey$respondent, "1=1; NA=0")

# Import file with candidates
Dail31candidates <- read.csv("data_ireland_2011-2016/dail_31_candidates.csv")
Dail31_Survey <- dplyr::left_join(Dail31candidates, Dail31_Survey, by = "pid")

# Recode gender
Dail31_Survey$female <- car::recode(Dail31_Survey$Gender, "'F'=1; 'M'=0; NA=NA")

# New variable for spending (divide by 1000)
Dail31_Survey$campaign_spending1000 <- (Dail31_Survey$campaign_spending)/1000


Dail31_Survey$wonseat <- car::recode(Dail31_Survey$wonseat, "NA=0")


# Keep variables needed for regressions ---
Dail31_Survey_subset <- dplyr::select(Dail31_Survey,
                               respondent,
                               wonseatCCS,
                               partyCCS,
                               ageCCS,
                               genderCCS,
                               gender,
                               quota,
                               incumbentPrevCCS,
                               use_callingvoters,
                               use_debating,
                               use_partyofficials,
                               use_appointmentsclubs,
                               use_townhallmeetings,
                               use_localnewspaper,
                               use_natnewspaper,
                               use_localtvradio,
                               use_nattvradio,
                               use_posters,
                               use_billboards,
                               use_adsinnews,
                               use_officehours,
                               use_socialgatherings,
                               use_flyers,
                               use_website,
                               use_emaillist,
                               use_campaignvideo,
                               use_onlinechats,
                               use_blog,
                               use_twitter,
                               use_classic,
                               use_material,
                               use_postmodern,
                               use_campaign,
                               use_ord_callingvoters,
                               use_ord_debating,
                               use_ord_internet,
                               use_ord_partyofficials,
                               use_ord_appointmentsclubs,
                               use_ord_townhallmeetings,
                               use_ord_localnewspaper,
                               use_ord_natnewspaper,
                               use_ord_localtvradio,
                               use_ord_nattvradio,
                               use_ord_total,
                               party_factor,
                               words_total,
                               vote_activity,
                               vote_activity2011,
                               vote_activity2012,
                               vote_activity2013,
                               vote_activity2014,
                               vote_activity2015,
                               vote_activity2016,
                               committee_chair,
                               wonseat,
                               minister,
                               minister_all_time,
                               opposition_leader,
                               speeches_total,
                               words_total,
                               age,
                               age_squared,
                               campaign_spending1000,
                               incumbent,
                               Win,
                               Party,
                               party_elected,
                               ranbefore,
                               female)
                               

dail31_reg <- Dail31_Survey_subset %>% 
  mutate(words_total_1000 = words_total/1000) %>% 
  mutate(government = car::recode(party_elected, "'Fine Gael'='Government'; 'Labour'='Government'; else='Opposition'")) %>% 
  mutate(country_id = "Ireland")

# Save this object as a csv file which can be used to replicate all regressions ---

write_csv(dail31_reg, "Dail31_final.csv")

